package org.thomias.exo1.udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Server {
	public void start() {
		try {
			DatagramSocket socket = new DatagramSocket(22222);
			try {
				byte[] empty = new byte[1024];
				byte[] receivedData = new byte[1024];
				DatagramPacket packet = new DatagramPacket(receivedData,
						receivedData.length);
				while (true) {
					System.arraycopy(empty, 0, receivedData, 0, 1024);
					socket.receive(packet);
					String sentence = new String(packet.getData()).trim();

					println(sentence);
					if (sentence.equals("quit")) {
						break;
					}
				}

			} finally {
				socket.close();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	private void println(String msg) {
		System.out.println("SERVER: " + msg);
	}
}
